package com.gitee.wsl.ext.bitcode


/** Extracts [count] bits at [offset] from [this] [Int] */
public fun Int.extract(offset: Int, count: Int): Int = (this ushr offset) and count.mask()
/** Extracts a bits at [offset] from [this] [Int] (returning a [Boolean]) */
inline fun Int.extract(offset: Int): Boolean = extract1(offset) != 0
/** Extracts a bits at [offset] from [this] [Int] (returning a [Boolean]) */
inline fun Int.extractBool(offset: Int): Boolean = extract1(offset) != 0
/** Extracts 1 bit at [offset] from [this] [Int] */
inline fun Int.extract1(offset: Int): Int = (this ushr offset) and 0b1
/** Extracts 2 bits at [offset] from [this] [Int] */
inline fun Int.extract2(offset: Int): Int = (this ushr offset) and 0b11
/** Extracts 3 bits at [offset] from [this] [Int] */
inline fun Int.extract3(offset: Int): Int = (this ushr offset) and 0b111
/** Extracts 4 bits at [offset] from [this] [Int] */
inline fun Int.extract4(offset: Int): Int = (this ushr offset) and 0b1111
/** Extracts 5 bits at [offset] from [this] [Int] */
inline fun Int.extract5(offset: Int): Int = (this ushr offset) and 0b11111
/** Extracts 6 bits at [offset] from [this] [Int] */
inline fun Int.extract6(offset: Int): Int = (this ushr offset) and 0b111111
/** Extracts 7 bits at [offset] from [this] [Int] */
inline fun Int.extract7(offset: Int): Int = (this ushr offset) and 0b1111111
/** Extracts 8 bits at [offset] from [this] [Int] */
inline fun Int.extract8(offset: Int): Int = (this ushr offset) and 0b11111111
/** Extracts 9 bits at [offset] from [this] [Int] */
inline fun Int.extract9(offset: Int): Int = (this ushr offset) and 0b111111111
/** Extracts 10 bits at [offset] from [this] [Int] */
inline fun Int.extract10(offset: Int): Int = (this ushr offset) and 0b1111111111
/** Extracts 11 bits at [offset] from [this] [Int] */
inline fun Int.extract11(offset: Int): Int = (this ushr offset) and 0b11111111111
/** Extracts 12 bits at [offset] from [this] [Int] */
inline fun Int.extract12(offset: Int): Int = (this ushr offset) and 0b111111111111
/** Extracts 13 bits at [offset] from [this] [Int] */
inline fun Int.extract13(offset: Int): Int = (this ushr offset) and 0b1111111111111
/** Extracts 14 bits at [offset] from [this] [Int] */
inline fun Int.extract14(offset: Int): Int = (this ushr offset) and 0b11111111111111
/** Extracts 15 bits at [offset] from [this] [Int] */
inline fun Int.extract15(offset: Int): Int = (this ushr offset) and 0b111111111111111
/** Extracts 16 bits at [offset] from [this] [Int] */
inline fun Int.extract16(offset: Int): Int = (this ushr offset) and 0b1111111111111111
/** Extracts 17 bits at [offset] from [this] [Int] */
inline fun Int.extract17(offset: Int): Int = (this ushr offset) and 0b11111111111111111
/** Extracts 18 bits at [offset] from [this] [Int] */
inline fun Int.extract18(offset: Int): Int = (this ushr offset) and 0b111111111111111111
/** Extracts 19 bits at [offset] from [this] [Int] */
inline fun Int.extract19(offset: Int): Int = (this ushr offset) and 0b1111111111111111111
/** Extracts 20 bits at [offset] from [this] [Int] */
inline fun Int.extract20(offset: Int): Int = (this ushr offset) and 0b11111111111111111111
/** Extracts 21 bits at [offset] from [this] [Int] */
inline fun Int.extract21(offset: Int): Int = (this ushr offset) and 0b111111111111111111111
/** Extracts 22 bits at [offset] from [this] [Int] */
inline fun Int.extract22(offset: Int): Int = (this ushr offset) and 0b1111111111111111111111
/** Extracts 23 bits at [offset] from [this] [Int] */
inline fun Int.extract23(offset: Int): Int = (this ushr offset) and 0b11111111111111111111111
/** Extracts 24 bits at [offset] from [this] [Int] */
inline fun Int.extract24(offset: Int): Int = (this ushr offset) and 0xFFFFFF
/** Extracts 25 bits at [offset] from [this] [Int] */
inline fun Int.extract25(offset: Int): Int = (this ushr offset) and 0b1111111111111111111111111
/** Extracts 26 bits at [offset] from [this] [Int] */
inline fun Int.extract26(offset: Int): Int = (this ushr offset) and 0b11111111111111111111111111
/** Extracts 27 bits at [offset] from [this] [Int] */
inline fun Int.extract27(offset: Int): Int = (this ushr offset) and 0b111111111111111111111111111
/** Extracts 28 bits at [offset] from [this] [Int] */
inline fun Int.extract28(offset: Int): Int = (this ushr offset) and 0b1111111111111111111111111111
/** Extracts 29 bits at [offset] from [this] [Int] */
inline fun Int.extract29(offset: Int): Int = (this ushr offset) and 0b11111111111111111111111111111
/** Extracts 30 bits at [offset] from [this] [Int] */
inline fun Int.extract30(offset: Int): Int = (this ushr offset) and 0b111111111111111111111111111111
/** Extracts 31 bits at [offset] from [this] [Int] */
inline fun Int.extract31(offset: Int): Int = (this ushr offset) and 0b1111111111111111111111111111111
/** Extracts 32 bits at [offset] from [this] [Int] */
inline fun Int.extract32(offset: Int): Int = (this ushr offset) and -1


/** Extracts [count] bits at [offset] from [this] [Int] sign-extending its result if [signed] is set to true */
public fun Int.extractSigned(offset: Int, count: Int, signed: Boolean): Int = if (signed) extractSigned(offset, count) else extract(offset, count)

/** Extracts [count] bits at [offset] from [this] [Int] sign-extending its result */
public fun Int.extractSigned(offset: Int, count: Int): Int = ((this ushr offset) and count.mask()).signExtend(count)
/** Extracts 8 bits at [offset] from [this] [Int] sign-extending its result */
public fun Int.extract8Signed(offset: Int): Int = (this ushr offset).toByte().toInt()
/** Extracts 16 bits at [offset] from [this] [Int] sign-extending its result */
public fun Int.extract16Signed(offset: Int): Int = (this ushr offset).toShort().toInt()

/** Extracts 8 bits at [offset] from [this] [Int] as [Byte] */
public fun Int.extractByte(offset: Int): Byte = (this ushr offset).toByte()
/** Extracts 16 bits at [offset] from [this] [Int] as [Short] */
public fun Int.extractShort(offset: Int): Short = (this ushr offset).toShort()

/** Extracts [count] at [offset] from [this] [Int] and convert the possible values into the range 0x00..[scale] */
public fun Int.extractScaled(offset: Int, count: Int, scale: Int): Int = (extract(offset, count) * scale) / count.mask()
/** Extracts [count] at [offset] from [this] [Int] and convert the possible values into the range 0.0..1.0 */
public fun Int.extractScaledf01(offset: Int, count: Int): Float = extract(offset, count).toFloat() / count.mask().toFloat()

/** Extracts [count] at [offset] from [this] [Int] and convert the possible values into the range 0x00..0xFF */
public fun Int.extractScaledFF(offset: Int, count: Int): Int = extractScaled(offset, count, 0xFF)
/** Extracts [count] at [offset] from [this] [Int] and convert the possible values into the range 0x00..0xFF (if there are 0 bits, returns [default]) */
public fun Int.extractScaledFFDefault(offset: Int, count: Int, default: Int): Int =
    if (count == 0) default else extractScaled(offset, count, 0xFF)
